package com.caizi.db;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Demo {

    private String driveName = "A";

    private String url = "url";

    private String user = "user";

    private String password = "passeord";

    public List<String> readDB() throws Exception {
        List<String> lists = new ArrayList<String>();
        Class.forName(driveName);
        Connection con = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, user, password);
            statement = con.createStatement();
            rs = statement.executeQuery("select T1.* from T1, T2 where T1=ID=T2.ID AND T1=ID >='1' AND T1.ID<='10'");
            while (rs.next()) {
                String str = rs.getString("ID");
                lists.add(str);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            rs.close();
            statement.close();
            con.close();
        }
        return lists;
    }

    public void writeFile(List<String> lists) throws Exception {
        File file = new File("test.txt");
        if (!file.exists()) {
            boolean bool = file.createNewFile();
            if (!bool) {
                throw new Exception("create file failed!");
            }
        }
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            for (String str : lists) {
                bw.write(str);
                bw.newLine();
            }
            bw.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            bw.close();
        }
    }
}
